﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.IO;

namespace LittleBee.CellStore.UserControls
{
	public partial class UCCellCode : UserControl
	{
		public class CodeComitEventArg : EventArgs
		{
			public CodeComitEventArg()
			{
				Code = "";
			}

			public CodeComitEventArg(string code)
			{
				Code = code;
			}
			public String Code { get; set; }
		}

		public delegate void CodeComitEvent(EventArgs arg);
		//public event CodeComitEvent OnCodeComit;
		public UCCellCode()
		{
			InitializeComponent();
		}

		public static int CompileCode(string strCode, CompilerParameters cp, out CompilerResults cr)
		{
			CSharpCodeProvider provider = new CSharpCodeProvider();

			cr = provider.CompileAssemblyFromSource(cp, strCode);

			return cr.Errors.Count;
		}

		private void UCCellCode_Load(object sender, EventArgs e)
		{
			StoreCell cell = (Tag as StoreCell)!;
			rtbCode.Text = cell.Code;

			rtbCompileResult.Text = CompileCode();
		}

		private string CompileCode()
		{
			CompilerParameters parameters = new CompilerParameters
			{
				GenerateExecutable = false,
				GenerateInMemory = false,
				OutputAssembly = Path.GetTempFileName()
			};
			parameters.ReferencedAssemblies.Add("libCell.dll");
            parameters.ReferencedAssemblies.Add("libIDAL.dll");
            parameters.ReferencedAssemblies.Add("System.dll");
			parameters.ReferencedAssemblies.Add("System.Core.dll");
			parameters.ReferencedAssemblies.Add("libdba.dll");
            parameters.ReferencedAssemblies.Add("libEngUnit.dll");
            parameters.ReferencedAssemblies.Add("Newtonsoft.Json.dll");
            parameters.ReferencedAssemblies.Add("libUtils.dll");

            StringBuilder sb = new StringBuilder();

			try
			{
				if (CompileCode(rtbCode.Text, parameters, out CompilerResults cr) == 0)
				{
					sb.Append("编译成功");
				}
				else
				{
					// Display compilation errors.
					Console.WriteLine("Errors building " + rtbCode.Text + " into " + cr.PathToAssembly);
					foreach (CompilerError ce in cr.Errors)
					{
						sb.Append(ce.ToString());
					}
				}
			}
			catch (Exception ex)
			{
				Console.WriteLine("Exception catched: " + ex.Message);
				Console.WriteLine("Stack info: " + ex.StackTrace);
				sb.Append(ex.Message).Append('\n').Append(ex.StackTrace);
			}

			return sb.ToString();
        }

		private void UCCellCode_Leave(object sender, EventArgs e)
		{
			StoreCell cell = (Tag as StoreCell)!;
			cell.Code = rtbCode.Text;
			//if (OnCodeComit != null)
			//	OnCodeComit(new CodeComitEventArg(rtbCode.Text));

		}

		private void rtbCode_Leave(object sender, EventArgs e)
		{
			StoreCell cell = (Tag as StoreCell)!;
			cell.Code = rtbCode.Text;
			//if (OnCodeComit != null)
			//	OnCodeComit(new CodeComitEventArg(rtbCode.Text));
		}
	}
}
